var fs = require("fs");
var path = require("path");
var config = require("./Config");
var os = require("os");
//整理install文件夹
var qudao = config.qudao;
var src = config.src;
var installtichu = config.installtichu;
var inputparam = config.inputparam;

function mkdirsSync(dirname) {
    if (fs.existsSync(dirname)) {
        return true;
    } else {
        if (mkdirsSync(path.dirname(dirname))) {
            fs.mkdirSync(dirname);
            return true;
        }
    }
}

function delFile(path, reservePath) {
    if (fs.existsSync(path)) {
        if (fs.statSync(path).isDirectory()) {
            let files = fs.readdirSync(path);
            files.forEach((file, index) => {
                let currentPath = path + "/" + file;
                if (fs.statSync(currentPath).isDirectory()) {
                    delFile(currentPath, reservePath);
                } else {
                    fs.unlinkSync(currentPath);
                }
            });
            if (path != reservePath) {
                fs.rmdirSync(path);
            }
        } else {
            fs.unlinkSync(path);
        }
    }
}

let allasset = fs.readFileSync(src + "asset_pack.json", "utf-8");
allasset = JSON.parse(allasset);
var projectmanifest = JSON.parse(fs.readFileSync("./hotupdata_install/" + qudao + "hall" + "/project.manifest", "utf-8"));
var versionmanifest = JSON.parse(fs.readFileSync("./hotupdata_install/" + qudao + "hall" + "/version.manifest", "utf-8"));
var versionsave = fs.readFileSync("./newversion.txt", "utf-8");
let dir = "./hotupdata_install/" + qudao + "hall" + "/" + versionsave;
var readDirtemp = function (dir) {
    var stat = fs.statSync(dir);
    if (!stat.isDirectory()) {
        return;
    }
    var subpaths = fs.readdirSync(dir);
    for (var i = 0; i < subpaths.length; ++i) {
        if (subpaths[i][0] === ".") {
            continue;
        }
        subpath = path.join(dir, subpaths[i]);
        stat = fs.statSync(subpath);
        if (stat.isDirectory()) {
            readDirtemp(subpath);
        } else if (stat.isFile()) {
            var platform = os.platform();
            var array = "";
            if (platform == "win32") {
                array = subpath.split('\\')
            } else {
                array = subpath.split('/')
            }

            if (array[array.length - 2] != "src") {
                uuid = array[array.length - 1].split(".")[0]
                for (var p in allasset) { //
                    if (allasset[p].uuid == uuid) {
                        if (allasset[p].dir) {
                            var flag = false
                            for (var s = 0; s < installtichu.length; s++) {
                                if (allasset[p].dir.indexOf(installtichu[s]) != -1) {
                                    flag = true;
                                    break;
                                }
                            }
                            if (flag) {
                                var key = allasset[p].path;
                                if (key != "" && subpath.indexOf(".json") == -1) {
                                    if (projectmanifest.assets[key]) {
                                        console.log("剔除的资源subpath  is ", subpath)
                                        delFile(subpath, "")
                                        delete projectmanifest.assets[key];
                                    }
                                } else { //import文件不处理
                                    // if (key.indexOf("7c211ea9-e0d6-4497-b49a-486c4f0a3da2")!= -1) {
                                    //     console.log("projectmanifest.assets[key] 4 ", projectmanifest.assets[key])
                                    // }
                                    // var id = allasset[p].uuid.substr(0,2);
                                    // var newkey = "res/import/"+id+"/"+allasset[p].uuid+".json"
                                    // console.log("id is ",newkey)
                                    // console.log("projectmanifest.assets[newkey]",projectmanifest.assets[newkey])
                                    // if(projectmanifest.assets[newkey])
                                    // {
                                    //     delFile(subpath,"")
                                    //     console.log("projectmanifest.assets[key]  is ",projectmanifest.assets[newkey])
                                    //     delete projectmanifest.assets[newkey];
                                    // }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}
readDirtemp(dir)
var versiont = projectmanifest.version;
var des = versiont.split(".")
var newversion = des[0] + "." + des[1] + "." + String(Number(des[2]) - 1)
var reg = new RegExp(versiont, "g");
//整理project.manifest文件
projectmanifest.packageUrl = projectmanifest.packageUrl.replace(reg, newversion);
projectmanifest.remoteManifestUrl = projectmanifest.remoteManifestUrl.replace(reg, newversion);
projectmanifest.remoteVersionUrl = projectmanifest.remoteVersionUrl.replace(reg, newversion);
projectmanifest.version = newversion;
//整理version.manifest文件
versionmanifest.packageUrl = versionmanifest.packageUrl.replace(reg, newversion);
versionmanifest.remoteManifestUrl = versionmanifest.remoteManifestUrl.replace(reg, newversion);
versionmanifest.remoteVersionUrl = versionmanifest.remoteVersionUrl.replace(reg, newversion);
versionmanifest.version = newversion;


delFile("./hotupdata_install/" + qudao + "hall" + "/project.manifest", "")
delFile("./hotupdata_install/" + qudao + "hall" + "/version.manifest", "")
mkdirsSync("./hotupdata_install/" + qudao + "hall");
fs.writeFileSync(
    "./hotupdata_install/" + qudao + "hall" + "/project.manifest",
    JSON.stringify(projectmanifest)
);
fs.writeFileSync(
    "./hotupdata_install/" + qudao + "hall" + "/version.manifest",
    JSON.stringify(versionmanifest)
);

//删除空文件夹


function readDirSync(filePath) {
    var pa = fs.readdirSync(filePath);
    pa.forEach(function (ele, index) {
        var info = fs.statSync(filePath + "/" + ele)
        if (info.isDirectory()) {
            // 如果是空文件夹 则执行删除操作
            if (isEmptyDir(filePath + "/" + ele)) {
                console.log('delete empty foder=> ' + ele);
                fs.rmdirSync(filePath + "/" + ele);
            } else {
                readDirSync(filePath + "/" + ele)
            }
        } else {
            //console.log('不是空文件夹不执行删除操作===>');
        }
    })
}

// 判断是否是空文件夹
function isEmptyDir(fPath) {
    var pa = fs.readdirSync(fPath);
    if (pa.length === 0) {
        return true;
    } else {
        return false;
    }
}

readDirSync("./hotupdata_install/" + qudao + "hall") // 以当前js文件所在目录进行遍历